/**
 * home.html page script
 * @author czp
 * date : 2014-04-11
 */
var defDay = 30;
var nowDay = new Date().format("yyyy-MM-dd");
var startDay = new Date( new Date() - defDay*24*3600*1000 ).format("yyyy-MM-dd");
var timeRange = startDay + " > " + nowDay;
var apiCountBarChart = echarts.init(document.getElementById('apiCountBar'));
var sensitiveItemPieChart = echarts.init(document.getElementById('sensitiveItemPie'));
var sensitiveItemTopBarChart = echarts.init(document.getElementById('sensitiveItemTopBar'));
var apiCountLineChart = echarts.init(document.getElementById('apiCountLine'));
sensitiveItemTopBarChart.showLoading({text: "数据加载中..."});
apiCountLineChart.showLoading({text: "数据加载中..."});
sensitiveItemPieChart.showLoading({text: "数据加载中..."});
apiCountBarChart.showLoading({text: "数据加载中..."});
drawApiCountLine(timeRange);
drawSensitiveItemTopBar(timeRange);
drawSensitiveItemPie(timeRange);
drawApiCountBar(timeRange);

function drawApiCountLine(timeRange) {
    $.ajax({
        url : contextPath + '/home/apiCountLine',
        type : 'POST',
        data : {"time_range" : null},
        success : function (data) {
            var xAxisBuf = printDay(timeRange, ">");
            var legend = [];
            var series = [];
            if(data !== null && data !== "") {
                for (var i = 0; i < data.buckets.length; i++) {
                    legend[i] = data.buckets[i].key;
                    var _data = [];
                    for (var j = 0; j < xAxisBuf.length; j++) {
                        _data[j] = 0;
                        for (var k = 0; k < data.buckets[i].es_time_groups.buckets.length; k++) {
                            if (xAxisBuf[j] === data.buckets[i].es_time_groups.buckets[k].key_as_string) {
                                _data[j] = data.buckets[i].es_time_groups.buckets[k].doc_count;
                                break;
                            }
                        }
                    }
                    series.push({
                        name: legend[i],
                        type: 'line',
                        areaStyle: {
                            normal: {
                                color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [{
                                    offset: 0,
                                    color: 'rgba(16,97,204, 0.3)'
                                }, {
                                    offset: 0.8,
                                    color: 'rgba(17,235,210, 0)'
                                }], false),
                                shadowColor: 'rgba(0, 0, 0, 0.1)',
                                shadowBlur: 10
                            }
                        },
                        data: _data
                    })
                }
            }
            var option = {
                title: {
                    text: '应用接口调用趋势'
                },
                tooltip : {
                    trigger: 'axis'
                },
                legend: {
                    type: 'scroll',
                    icon: 'rect',
                    itemWidth: 14,
                    itemHeight: 5,
                    itemGap: 13,
                    right: '25%',
                    left: '40%',
                    data: legend
                },
                dataZoom: [{
                    type: 'inside'
                }],
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                toolbox: {
                    show : true,
                    feature : {
                        mark : {show: true},
                        dataView : {show: true, readOnly: false},
                        magicType : {show: true, type: ['line', 'bar']},
                        restore : {show: true},
                        saveAsImage : {show: true}
                    }
                },
                calculable : true,
                xAxis : [{
                    type : 'category',
                    boundaryGap : false,
                    data : xAxisBuf
                }],
                yAxis : [
                    { type : 'value' }
                ],
                series : series
            };
            apiCountLineChart.setOption(option);
            apiCountLineChart.hideLoading();
        },
        error: function () {
            apiCountLineChart.showLoading({text: "数据加载中..."});
            setInterval(" drawApiCountLine(timeRange)", 20000);
        }
    });
}

function drawSensitiveItemTopBar(timeRange) {
    $.ajax({
        url: contextPath + '/home/sensitiveItemTopBar',
        type: 'POST',
        success: function (data) {
            var names = [];
            var numbers = [];
            data.forEach(function (m) {
                names.push(m.key);
                numbers.push(m.doc_count)
            });
            var option = {
                title: {
                    text: '敏感项保护次数TOP',
                    textStyle: {fontSize: 20}
                },
                tooltip: {
                    trigger: 'axis',
                    backgroundColor: 'rgba(255,255,255,0.8)',
                    extraCssText: 'box-shadow: 0 0 8px rgba(0, 0, 0, 0.3)',
                    textStyle: {color: '#6a717b'}
                },
                toolbox: {
                    show : true,
                    feature : {
                        mark : {show: true},
                        dataView : {show: true, readOnly: false},
                        magicType : {show: true, type: ['line', 'bar']},
                        restore : {show: true},
                        saveAsImage : {show: true}
                    }
                },
                grid: {left: '3%', right: '4%', bottom: '3%', containLabel: true},
                yAxis: [{
                    type: 'category',
                    data: names,
                    axisTick: {alignWithLabel: true},
                    axisLabel: {
                        margin: 10,
                        textStyle: {fontSize: 12, color: '#94999f'}
                    },
                    axisLine: {lineStyle: {color: '#dbe0e6'}}
                }],
                xAxis: [{
                    type: 'value',
                    axisLabel: {
                        margin: 10,
                        textStyle: {fontSize: 12, color: '#94999f'}
                    },
                    axisLine: {lineStyle: {color: '#fff'}},
                    splitLine: {lineStyle: {color: '#dbe0e6'}}
                }],
                backgroundColor: '#ffffff',
                series: [{
                    name: '保护次数',
                    type: 'bar',
                    barWidth: 15,
                    data: numbers,
                    label: {
                        normal: {
                            show: true,
                            position: 'insideRight',
                            textStyle: {color: 'white'}
                        }
                    },
                    itemStyle: {
                        normal: {
                            color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [{
                                offset: 0,
                                color: '#64bdc8'
                            }, {
                                offset: 1,
                                color: '#00c484'
                            }], false),
                            barBorderRadius: [0, 15, 15, 0],
                            shadowColor: 'rgba(0,0,0,0.1)',
                            shadowBlur: 3,
                            shadowOffsetY: 3
                        }
                    }
                }]
            };
            sensitiveItemTopBarChart.setOption(option);
            sensitiveItemTopBarChart.hideLoading();
        },
        error : function () {
            sensitiveItemTopBarChart.showLoading({text : "数据加载中..."});
            setInterval("drawSensitiveItemTopBar(timeRange)", 20000);
        }
    });
}

function drawSensitiveItemPie(timeRange) {
    $.ajax({
        url: contextPath + '/home/sensitiveItemPie',
        type: 'POST',
        success: function (data) {
            var items = [];
            var listK = [];
            var n = 0;
            data.forEach(function (m) {
                listK[n] = m.c_ds_item_name;
                items.push({
                    value: m.num,
                    name: listK[n]
                });
                n ++;
            });
            var option = {
                title: {
                    text: '敏感项占比',
                    textStyle: {fontSize: 20}
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{b} : {c}列(占比{d}%)"
                },
                legend: {
                    type: 'scroll',
                    orient: 'vertical',
                    left: 'left',
                    top: 40,
                    bottom: 5,
                    data: listK
                },
                toolbox: {
                    show : true,
                    feature : {
                        mark : {show: true},
                        dataView : {show: true, readOnly: false},
                        restore : {show: true},
                        saveAsImage : {show: true}
                    }
                },
                series: [{
                    name: '敏感项统计',
                    type: 'pie',
                    radius: '60%',
                    center: ['60%', '50%'],
                    labelLine: { show: false },
                    label: { show: false },
                    data: items,
                    itemStyle: {
                        emphasis: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }]
            };
            sensitiveItemPieChart.setOption(option);
            sensitiveItemPieChart.hideLoading();
        }, // end success function
        error: function() {
            sensitiveItemPieChart.showLoading({text : "数据加载错误！！！"});
        }
    });
}

function drawApiCountBar(timeRange) {
    $.ajax({
        url: contextPath + '/home/apiCountBar',
        type: 'POST',
        success: function (data) {
            if (data === null) return;
            var legend = [];
            var xAxis = data.apis;
            var series = [];
            for (var key in data.map) {
                legend.push(key);
                series.push({
                    name: key,
                    type: 'bar',
                    stack: '总量',
                    label: {
                        normal: {
                            show: true,
                            position: 'insideRight'
                        }
                    },
                    data: data.map[key]
                })
            }
            apiCountBarChart.showLoading({text: "数据加载中..."});
            var option = {
                title: {
                    text: '当前自然月接口被调用次数',
                    textStyle: {fontSize: 20}
                },
                tooltip : {
                    trigger: 'axis',
                    axisPointer : {
                        type : 'shadow'
                    }
                },
                toolbox: {
                    show : true,
                    feature : {
                        mark : {show: true},
                        dataView : {show: true, readOnly: false},
                        restore : {show: true},
                        saveAsImage : {show: true}
                    }
                },
                legend: {
                    type: 'scroll',
                    icon: 'rect',
                    itemWidth: 14,
                    itemHeight: 5,
                    itemGap: 13,
                    right: '25%',
                    left: '40%',
                    data: legend
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis:  {
                    type: 'category',
                    data: xAxis
                },
                yAxis: {
                    type: 'value'
                },
                series: series
            };
            apiCountBarChart.setOption(option);
            apiCountBarChart.hideLoading();
        }, // end success function
        error: function() {
            apiCountBarChart.showLoading({text: "数据加载中..."});
            setInterval("drawApiCountBar(timeRange)", 20000);
        }
    });
}
